package com.easytouch.vmhdl.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.easytouch.core.DriverThread;
import com.easytouch.core.utils.SystemTools;
import com.easytouch.vmhdl.AppApplication;
import com.easytouch.vmhdl.R;
import com.easytouch.vmhdl.activity.MainActivity;
import com.easytouch.vmhdl.activity.ManagerActivity;
import com.easytouch.vmhdl.service.DriverService;

/**
 * Created by gulin on 2018/1/11.
 */

public class MenuFragment extends BaseMgrFragment implements View.OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_menu, container, false);
        setTitle(R.string.mgr_menu_title);

        Button btAdd = view.findViewById(R.id.bt_add);
        btAdd.setOnClickListener(this);
        Button btConfig = view.findViewById(R.id.bt_config);
        btConfig.setOnClickListener(this);
        Button btWarranty = view.findViewById(R.id.bt_warranty);
        btWarranty.setOnClickListener(this);
        Button btTest = view.findViewById(R.id.bt_test);
        btTest.setOnClickListener(this);
        Button btUpdate = view.findViewById(R.id.bt_update);
        btUpdate.setOnClickListener(this);
        Button btReboot = view.findViewById(R.id.bt_reboot);
        btReboot.setOnClickListener(this);
        Button btExit = view.findViewById(R.id.bt_exit);
        btExit.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                goToFragment(ManagerActivity.FRAGMENT_ADD, null);
                break;
            case R.id.bt_config:
                goToFragment(ManagerActivity.FRAGMENT_CONFIG, null);
                break;
            case R.id.bt_warranty:
                goToFragment(ManagerActivity.FRAGMENT_WARRANTY, null);
                break;
            case R.id.bt_test:
                goToFragment(ManagerActivity.FRAGMENT_TEST, null);
                break;
            case R.id.bt_update:
                goToFragment(ManagerActivity.FRAGMENT_UPDATE, null);
                break;
            case R.id.bt_reboot:
                onReboot();
                break;
            case R.id.bt_exit:
                onExit();
                break;
        }
    }

    private void onReboot()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.reboot);
        builder.setTitle(R.string.info);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                SystemTools.reboot();
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        builder.create().show();
    }

    private void onExit()
    {
        //弹出对话框，提示是要退出加货界面还是退出整个软件
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.exit);
        builder.setTitle(R.string.info);
        builder.setPositiveButton(R.string.exit_manager, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                exitManager();
            }
        });
        builder.setNegativeButton(R.string.exit_app, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                exitApp();
            }
        });
        builder.create().show();
    }

    /**
     * 退出加货软件
     */
    public void exitManager()
    {
        ManagerActivity activity = (ManagerActivity)this.getActivity();
        if(activity != null)
        {
            activity.cleanUp();
            Intent intent = new Intent(activity, MainActivity.class);
            startActivity(intent);
            activity.finish();
        }

    }

    /**
     * 退出整个软件
     */
    public void exitApp()
    {
        DriverThread.getInstance().stopThread();
        Intent service = new Intent(AppApplication.context, DriverService.class);
        AppApplication.context.stopService(service);
        SystemTools.showSystemBar();
        ManagerActivity activity = (ManagerActivity)this.getActivity();
        if(activity != null)
        {
            activity.cleanUp();
            activity.finish();
        }
    }
}
